//
//  MeViewController.m
//  AILove
//
//  Created by 甘世清 on 2023/12/15.
//

#import "MeViewController.h"
#import "LoginViewController.h"
#import "MeTopView.h"
#import "MeTableViewCell.h"
#import "guanyuViewController.h"
#import "FeedbackViewController.h"
#import "ZhuxiaoViewController.h"
#import "VipViewController.h"
#import "XieYiView.h"
@interface MeViewController ()<UITableViewDelegate,UITableViewDataSource,TZImagePickerControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic , strong) MeTopView * topView;
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@end

@implementation MeViewController
- (void)SQ_viewWillAppear:(BOOL)animated{
    [self SQ_Header];
}
- (void)viewDidLoad {
    [super viewDidLoad];
 
    // Do any additional setup after loading the view.
}

- (void)SQ_UIViewForSelfView{
    self.tableView.tableHeaderView = self.topView;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, FootHeight, 0));
    }];
    [self SQ_JsonArray];
}
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [HttpTool post:Url6 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        UserModel * mo = [UserModel share];
        UserModel* model = [UserModel mj_objectWithKeyValues:httpModel.data];
        model.token = mo.token;
        model.score = mo.score;
        [UserModel updateLocalUserModel:model];
        [self SQ_JsonArray];
        [self.topView SQ_model];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
}
-(void)SQ_JsonArray{
    [self.jsonArray removeAllObjects];
    NSArray * array = @[@"投诉反馈",@"协议内容",@"关于我们",@"注销账号",@"退出登录"];
    for (NSString * string in array) {
        [self.jsonArray addObject:string];
    }
   
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeTableViewCell"];
    if (!cell) {
        cell = [[MeTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"MeTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.name =  [self.jsonArray objectAtIndex:indexPath.item];
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  62;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString * name = [self.jsonArray objectAtIndex:indexPath.item];
     if ([name isEqualToString:@"投诉反馈"]) {
         FeedbackViewController * vc = [[FeedbackViewController alloc]init];
         
         [self SQ_pushViewControllerFull:vc];
    }else  if ([name isEqualToString:@"协议内容"]) {
        [self SQ_xieyiView];
    }else  if ([name isEqualToString:@"关于我们"]) {
        guanyuViewController * vc = [[guanyuViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    }else  if ([name isEqualToString:@"注销账号"]) {
        ZhuxiaoViewController * vc  =[[ZhuxiaoViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    }else  if ([name isEqualToString:@"退出登录"]) {
        [self SQ_tuichudenglu];
       
    }
}

-(void)SQ_tuichudenglu{
    WS(weakSelf);
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"退出后不会删除任何历史数据") message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * co1 = [UIAlertAction actionWithTitle:SQNSLocalString(@"退出幻想社交") style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [UserModel clearLocalUserModel];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[LoginViewController alloc]init]];
         weakSelf.view.window.rootViewController = nav;
    }];
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString( @"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co1];
    [alert addAction:co2];
    [self presentViewController:alert animated:YES completion:nil];
    
}
-(void)SQ_gotoVip{
    VipViewController * vc = [[VipViewController alloc]init];
    [self SQ_pushViewControllerFull:vc];
}
-(void)SQ_xieyiView{
    XieYiView * view = [[XieYiView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
    LSTPopView * pop = [LSTPopView initWithCustomView:view
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleCenter;
    pop.bgAlpha = 0.5;
    [pop pop];
    LSTPopViewWK(pop)
    [view.leftButton setTouchBlock:^(SQCustomButton * _Nonnull button) {
       [wk_pop dismiss];
   }];
}

-(void)SQ_headerButtonClick{
    WS(weakSelf);
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"设置头像") message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * co0 = [UIAlertAction actionWithTitle:SQNSLocalString(@"拍照") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_leftButtonClick];
    }];
    UIAlertAction * co1 = [UIAlertAction actionWithTitle:SQNSLocalString(@"相册") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_rightButtonClick];
    }];
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co0];
    [alert addAction:co1];
    [alert addAction:co2];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)SQ_leftButtonClick{
    WS(weakSelf);
    _imagePickerVc = [[UIImagePickerController alloc] init];
    _imagePickerVc.delegate = self;
    _imagePickerVc.allowsEditing = YES;
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: sourceType]) {
        weakSelf.imagePickerVc.sourceType = sourceType;
        [self presentViewController:weakSelf.imagePickerVc animated:YES completion:nil];
    } else {
        NSLog(@"模拟器中无法打开照相机,请在真机中使用");
    }
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    WS(weakSelf);
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage *photo = [info objectForKey:UIImagePickerControllerEditedImage];
        if (photo == nil)
            {
                photo = [info objectForKey:UIImagePickerControllerOriginalImage];
            }
        [weakSelf SQ_caijian:photo];
    }else
    {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"相册数据异常")];
    }
}


-(void)SQ_rightButtonClick{
    WS(weakSelf);
    TZImagePickerController * vc = [[TZImagePickerController alloc]init];
    vc.maxImagesCount = 1;
    [self presentViewController:vc animated:YES completion:nil];
    [vc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        [weakSelf SQ_caijian:photos.firstObject ];
    }];
}
//上传相册
-(void)SQ_caijian:(UIImage*)image {
    WS(weakSelf);
    //上传头像
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    [HttpTool SQ_Image:image successBlock:^(BaseModel * _Nonnull httpModel) {
        [weakSelf SQ_setUpName:httpModel.data];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_setUpName:(NSString*)header{
    if (![header isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"头像上传错误")];
        return;
    }
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    
    [dic setObject:header forKey:@"portrait"];
    [HttpTool post:Url5 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        UserModel * model = [ UserModel share];
        model.portrait = header;
        [UserModel updateLocalUserModel:model];
        [self.topView SQ_model];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

-(void)SQ_zuanshi{
    ChongZhiView * vc = [[ChongZhiView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:nil
                                             popStyle:LSTPopStyleFade dismissStyle:LSTDismissStyleFade];
    pop.hemStyle = LSTHemStyleCenter;
//    pop.isStack = YES;
    [pop pop];
    LSTPopViewWK(pop)
    [vc.guanbi setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [vc setCZBlock:^(BOOL isOk) {
        [wk_pop dismiss];
        [weakSelf SQ_Header];
    }];
}


- (MeTopView *)topView{
    if (!_topView) {
        _topView = [[MeTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 330)];
        [_topView.vipButton addTarget:self action:@selector(SQ_gotoVip) forControlEvents:UIControlEventTouchUpInside];
        [_topView.headerBtn addTarget:self action:@selector(SQ_headerButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_topView.zuanshiButton addTarget:self action:@selector(SQ_zuanshi) forControlEvents:UIControlEventTouchUpInside];
    }
    return _topView;
}

@end
